﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Threading;

using PickGold.Callers;
using PickGold.Handler;

namespace PickGold.Arguments
{
	/// <summary>
	/// 异步包装器，主要用于附加额外的
	/// </summary>
	[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	public class SyncResultEventArgs : EventArgs, IAsyncResult
	{
		/// <summary>
		/// 用户定义的对象，它限定或包含关于异步操作的信息。
		/// </summary>
		public object AsyncState;
		/// <summary>
		/// 用于等待异步操作完成的 WaitHandle 对象。
		/// </summary>
		public WaitHandle AsyncWaitHandle;
		/// <summary>
		/// 一个值，该值指示异步操作是否同步完成。
		/// </summary>
		public bool CompletedSynchronously;
		/// <summary>
		/// 一个值，该值指示异步操作是否已完成。
		/// </summary>
		public bool IsCompleted;

		/// <summary/>
		public SyncResultEventArgs()
		{
			this.AsyncState = null;
			this.AsyncWaitHandle = null;
			this.CompletedSynchronously = true;
			this.IsCompleted = true;
		}

		/// <summary/>
		public SyncResultEventArgs(bool isCompleted)
		{
			this.AsyncState = null;
			this.AsyncWaitHandle = null;
			this.CompletedSynchronously = true;
			this.IsCompleted = isCompleted;
		}

		/// <summary/>
		public SyncResultEventArgs(bool isCompleted, object asyncState)
		{
			this.AsyncState = asyncState;
			this.AsyncWaitHandle = null;
			this.CompletedSynchronously = true;
			this.IsCompleted = isCompleted;
		}

		/// <summary/>
		public SyncResultEventArgs(bool isCompleted, bool completedSynchronously)
		{
			this.AsyncState = null;
			this.AsyncWaitHandle = null;
			this.CompletedSynchronously = completedSynchronously;
			this.IsCompleted = isCompleted;
		}

		/// <summary/>
		public SyncResultEventArgs(bool isCompleted, bool completedSynchronously, object asyncState)
		{
			this.AsyncState = asyncState;
			this.AsyncWaitHandle = null;
			this.CompletedSynchronously = completedSynchronously;
			this.IsCompleted = isCompleted;
		}

		/// <summary/>
		public SyncResultEventArgs(bool isCompleted, bool completedSynchronously, object asyncState, EventWaitHandle asyncWaitHandle)
		{
			this.AsyncState = asyncState;
			this.AsyncWaitHandle = asyncWaitHandle;
			this.CompletedSynchronously = completedSynchronously;
			this.IsCompleted = isCompleted;
		}

		/// <summary/>
		public SyncResultEventArgs(IAsyncResult result)
		{
			this.AsyncState = result.AsyncState;
			this.AsyncWaitHandle = result.AsyncWaitHandle;
			this.CompletedSynchronously = result.CompletedSynchronously;
			this.IsCompleted = result.IsCompleted;
		}

		object IAsyncResult.AsyncState
		{
			get
			{
				return this.AsyncState;
			}
		}

		WaitHandle IAsyncResult.AsyncWaitHandle
		{
			get
			{
				return this.AsyncWaitHandle;
			}
		}

		bool IAsyncResult.CompletedSynchronously
		{
			get
			{
				return this.CompletedSynchronously;
			}
		}

		bool IAsyncResult.IsCompleted
		{
			get
			{
				return this.IsCompleted;
			}
		}
	}
}
